package net.htjs.mobile.qxoa;

import java.util.List;

import net.htjs.mobile.qxoa.apn.util.ApnUtility;
import net.htjs.mobile.qxoa.util.L;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;

public class BaseActivity extends Activity {

	protected net.htjs.mobile.qxoa.apn.util.ApnUtility apnutility = null;
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}
	
	public void shutDown(){
    	System.exit(1);
    }
	@Override
	public void onCreate(Bundle savedInstanceState) {

		apnutility = new ApnUtility(this);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * 程序是否在前台运行
	 * 
	 * @return
	 */
	public boolean isAppOnForeground() {

		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;

		for (RunningAppProcessInfo appProcess : appProcesses) {
			// The name of the process that this object is associated with.
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}
		return false;
	}

	// 编辑APN内容
	protected void editMobileApn() {
		if(!L.isUseAPN)return;
		int id = -1;
		Uri uri = Uri.parse("content://telephony/carriers");
		ContentResolver resolver = getContentResolver();
		Cursor c = resolver.query(uri, new String[] { "_id", "name", "apn","type" },
				"apn like '%"+L.APNNAME+"%'", null, null);

		// 该项APN存在
		 
		if (c != null && c.moveToNext()) {
			id = c.getShort(c.getColumnIndex("_id"));
			String name = c.getString(c.getColumnIndex("name"));
			String apn = c.getString(c.getColumnIndex("apn"));
			String type=c.getString(c.getColumnIndex("type"));
			Log.v("APN", id + name + apn);
			
			if(apnutility.isNyoa(new String[]{name,apn,type})){
				apnutility.setDefaultApn(id);
			}else{
				apnutility.Delete_Apn(id);
			}
			
		} else {
			apnutility.setDefaultApn(apnutility.AddYidongApn());
		}
	}
}